package com.example.concurrentprogramming8016.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author liyanhao
 * @Date 2023-01-08 15:08
 */
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new Thread(() -> {
            System.out.println("通过Runnable方式执行任务");
        }).start();
        FutureTask futureTask = new FutureTask<>(new Callable() {
            @Override
            public Object call() throws Exception {
                System.out.println("通过Callable方式执行任务");
                TimeUnit.SECONDS.sleep(3);
                return "返回任务结果";
            }
        });
        new Thread(futureTask).start();
        System.out.println(futureTask.get());
    }
}
